package jmine.tec.proxy.builder;

import jmine.tec.proxy.ProxyBuilder;

/**
 * @author MarceloT
 */
final class ProxyBuilderProvider {

    public static final String JAVASSIST_SELECTOR_NAME = "javassist.util.proxy.ProxyFactory";

    public static final String CGLIB_SELECTOR_CLASS = "net.sf.cglib.proxy.Enhancer";

    /**
     * 
     */
    private ProxyBuilderProvider() {
    }

    /**
     * @return {@link ProxyBuilderFactory}
     */
    public static ProxyBuilderFactory selectProxyBuilderFactory() {
        if (exists(CGLIB_SELECTOR_CLASS)) {
            return new CGLibProxyFactoryBuilder();
        }
        if (exists(JAVASSIST_SELECTOR_NAME)) {
            return new JavassistProxyFactoryBuilder();
        }
        return new ProxyBuilderFactory() {

            public ProxyBuilder newProxyBuilder() {
                return new JDKProxyBuilder();
            }

        };
    }

    /**
     * @param classname the classname
     * @return boolean
     */
    public static boolean exists(String classname) {
        try {
            Class.forName(classname, false, ProxyBuilderProvider.class.getClassLoader());
        } catch (ClassNotFoundException e) {
            return false;
        }
        return true;
    }

}
